home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / PL 2.0 SupplementDoc Folder.sit / PL 2.0 SupplementDoc Folder / Documentation / Chapter 15. Lists < prev    next >
Text File  |  1995-03-27  |  46KB  |  1,244 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 15. Lists
  5.  
  6. A cons, or dotted pair, is a compound data object having two components called
  7. the car and cdr. Each component may be any Lisp object. A list is a chain of
  8. conses linked by cdr fields; the chain is terminated by some atom (a non-cons
  9. object). An ordinary list is terminated by nil, the empty list (also written
  10. ()). A list whose cdr chain is terminated by some non-nil atom is called a
  11. dotted list.
  12.  
  13. The recommended predicate for testing for the end of a list is endp.
  14.  
  15. -------------------------------------------------------------------------------
  16.  
  17.    *  Conses
  18.    *  Lists
  19.    *  Alteration of List Structure
  20.    *  Substitution of Expressions
  21.    *  Using Lists as Sets
  22.    *  Association Lists
  23.  
  24. -------------------------------------------------------------------------------
  25.  
  26. 15.1. Conses
  27.  
  28. These are the basic operations on conses viewed as pairs rather than as the
  29. constituents of a list.
  30.  
  31. [Function]
  32. car list
  33.  
  34. This returns the car of list, which must be a cons or (); that is, list must
  35. satisfy the predicate listp. By definition, the car of () is (). If the cons is
  36. regarded as the first cons of a list, then car returns the first element of the
  37. list. For example:
  38.  
  39. (car '(a b c)) => a
  40.  
  41. See first. The car of a cons may be altered by using rplaca or setf.
  42.  
  43. [Function]
  44. cdr list
  45.  
  46. This returns the cdr of list, which must be a cons or (); that is, list must
  47. satisfy the predicate listp. By definition, the cdr of () is (). If the cons is
  48. regarded as the first cons of a list, then cdr returns the rest of the list,
  49. which is a list with all elements but the first of the original list. For
  50. example:
  51.  
  52. (cdr '(a b c)) => (b c)
  53.  
  54. See rest. The cdr of a cons may be altered by using rplacd or setf.
  55.  
  56. [Function]
  57. caar list
  58. cadr list
  59. cdar list
  60. cddr list
  61. caaar list
  62. caadr list
  63. cadar list
  64. caddr list
  65. cdaar list
  66. cdadr list
  67. cddar list
  68. cdddr list
  69. caaaar list
  70. caaadr list
  71. caadar list
  72. caaddr list
  73. cadaar list
  74. cadadr list
  75. caddar list
  76. cadddr list
  77. cdaaar list
  78. cdaadr list
  79. cdadar list
  80. cdaddr list
  81. cddaar list
  82. cddadr list
  83. cdddar list
  84. cddddr list
  85.  
  86. All of the compositions of up to four car and cdr operations are defined as
  87. separate Common Lisp functions. The names of these functions begin with c and
  88. end with r, and in between is a sequence of a and d letters corresponding to
  89. the composition performed by the function. For example:
  90.  
  91. (cddadr x) is the same as (cdr (cdr (car (cdr x))))
  92.  
  93. If the argument is regarded as a list, then cadr returns the second element of
  94. the list, caddr the third, and cadddr the fourth. If the first element of a
  95. list is a list, then caar is the first element of the sublist, cdar is the rest
  96. of that sublist, and cadar is the second element of the sublist, and so on.
  97.  
  98. As a matter of style, it is often preferable to define a function or macro to
  99. access part of a complicated data structure, rather than to use a long car/cdr
  100. string. For example, one might define a macro to extract the list of parameter
  101. variables from a lambda-expression:
  102.  
  103. (defmacro lambda-vars (lambda-exp) `(cadr ,lambda-exp))
  104.  
  105. and then use lambda-vars for this purpose instead of cadr. See also defstruct,
  106. which will automatically define new record data types and access functions for
  107. instances of them.
  108.  
  109. Any of these functions may be used to specify a place for setf.
  110.  
  111. [Function]
  112. cons x y
  113.  
  114. cons is the primitive function to create a new cons whose car is x and whose
  115. cdr is y. For example:
  116.  
  117. (cons 'a 'b) => (a . b)
  118. (cons 'a (cons 'b (cons 'c '()))) => (a b c)
  119. (cons 'a '(b c d)) => (a b c d)
  120.  
  121. cons may be thought of as creating a cons, or as adding a new element to the
  122. front of a list.
  123.  
  124. [Function]
  125. tree-equal x y &key :test :test-not
  126.  
  127. This is a predicate that is true if x and y are isomorphic trees with identical
  128. leaves, that is, if x and y are atoms that satisfy the test (by default eql),
  129. or if they are both conses and their car's are tree-equal and their cdr's are
  130. tree-equal. Thus tree-equal recursively compares conses (but not any other
  131. objects that have components). See equal, which does recursively compare
  132. certain other structured objects, such as strings.
  133.  
  134. [change_begin]
  135. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  136. user side effects; see section 7.9.
  137. [change_end]
  138.  
  139. -------------------------------------------------------------------------------
  140.  
  141. 15.2. Lists
  142.  
  143. The following functions perform various operations on lists.
  144.  
  145. [change_begin]
  146. The list is one of the original Lisp data types. The very name ``Lisp'' is an
  147. abbreviation for ``LISt Processing.''
  148. [change_end]
  149.  
  150. [Function]
  151. endp object
  152.  
  153. The predicate endp is the recommended way to test for the end of a list. It is
  154. false of conses, true of nil, and an error for all other arguments.
  155.  
  156. -------------------------------------------------------------------------------
  157. Implementation note: Implementations are encouraged to signal an error,
  158. especially in the interpreter, for a non-list argument. The endp function is
  159. defined so as to allow compiled code to perform simply an atom check or a null
  160. check if speed is more important than safety.
  161. -------------------------------------------------------------------------------
  162.  
  163. [Function]
  164. list-length list
  165.  
  166. list-length returns, as an integer, the length of list. list-length differs
  167. from length when the list is circular; length may fail to return, whereas
  168. list-length will return nil. For example:
  169.  
  170. (list-length '()) => 0
  171. (list-length '(a b c d)) => 4
  172. (list-length '(a (b c) d)) => 3
  173. (let ((x (list 'a b c)))
  174.   (rplacd (last x) x)
  175.   (list-length x)) => nil
  176.  
  177. list-length could be implemented as follows:
  178.  
  179. (defun list-length (x)
  180.   (do ((n 0 (+ n 2))            ;Counter
  181.        (fast x (cddr fast))     ;Fast pointer: leaps by 2
  182.        (slow x (cdr slow)))     ;Slow pointer: leaps by 1
  183.       (nil)
  184.     ;; If fast pointer hits the end, return the count.
  185.     (when (endp fast) (return n))
  186.     (when (endp (cdr fast)) (return (+ n 1)))
  187.     ;; If fast pointer eventually equals slow pointer,
  188.     ;;  then we must be stuck in a circular list.
  189.     ;; (A deeper property is the converse: if we are
  190.     ;;  stuck in a circular list, then eventually the
  191.     ;;  fast pointer will equal the slow pointer.
  192.     ;;  That fact justifies this implementation.)
  193.     (when (and (eq fast slow) (> n 0)) (return nil))))
  194.  
  195. See length, which will return the length of any sequence.
  196.  
  197. [Function]
  198. nth n list
  199.  
  200. (nth n list) returns the nth element of list, where the car of the list is the
  201. ``zeroth'' element. The argument n must be a non-negative integer. If the
  202. length of the list is not greater than n, then the result is (), that is, nil.
  203. (This is consistent with the idea that the car and cdr of () are each ().) For
  204. example:
  205.  
  206. (nth 0 '(foo bar gack)) => foo
  207. (nth 1 '(foo bar gack)) => bar
  208. (nth 3 '(foo bar gack)) => ()
  209.  
  210. -------------------------------------------------------------------------------
  211. Compatibility note: This is not the same as the Interlisp function called nth,
  212. which is similar to but not exactly the same as the Common Lisp function
  213. nthcdr. This definition of nth is compatible with Lisp Machine Lisp and NIL
  214. (New Implementation of Lisp). Also, some people have used macros and functions
  215. called nth of their own in their old MacLisp programs, which may not work the
  216. same way.
  217. -------------------------------------------------------------------------------
  218.  
  219. nth may be used to specify a place to setf; when nth is used in this way, the
  220. argument n must be less than the length of the list.
  221.  
  222. Note that the arguments to nth are reversed from the order used by most other
  223. sequence selector functions such as elt.
  224.  
  225. [Function]
  226. first list
  227. second list
  228. third list
  229. fourth list
  230. fifth list
  231. sixth list
  232. seventh list
  233. eighth list
  234. ninth list
  235. tenth list
  236.  
  237. These functions are sometimes convenient for accessing particular elements of a
  238. list. first is the same as car, second is the same as cadr, third is the same
  239. as caddr, and so on. Note that the ordinal numbering used here is one-origin,
  240. as opposed to the zero-origin numbering used by nth:
  241.  
  242. (fifth x) == (nth 4 x)
  243.  
  244. setf may be used with each of these functions to store into the indicated
  245. position of a list.
  246.  
  247. [Function]
  248. rest list
  249.  
  250. rest means the same as cdr but mnemonically complements first. setf may be used
  251. with rest to replace the cdr of a list with a new value.
  252.  
  253. [Function]
  254. nthcdr n list
  255.  
  256. (nthcdr n list) performs the cdr operation n times on list, and returns the
  257. result. For example:
  258.  
  259. (nthcdr 0 '(a b c)) => (a b c)
  260. (nthcdr 2 '(a b c)) => (c)
  261. (nthcdr 4 '(a b c)) => ()
  262.  
  263. In other words, it returns the nth cdr of the list.
  264.  
  265. -------------------------------------------------------------------------------
  266. Compatibility note: This is similar to the Interlisp function nth, except that
  267. the Interlisp function is one-based instead of zero-based.
  268. -------------------------------------------------------------------------------
  269.  
  270. (car (nthcdr n x)) == (nth n x)
  271.  
  272. [change_begin]
  273. X3J13 voted in January 1989 (ARGUMENTS-UNDERSPECIFIED)   to clarify that the
  274. argument n must be a non-negative integer.
  275. [change_end]
  276.  
  277. [old_change_begin]
  278.  
  279. [Function]
  280. last list
  281.  
  282. last returns the last cons (not the last element!) of list. If list is (), it
  283. returns (). For example:
  284.  
  285. (setq x '(a b c d))
  286. (last x) => (d)
  287. (rplacd (last x) '(e f))
  288. x => '(a b c d e f)
  289. (last '(a b c . d)) => (c . d)
  290.  
  291. [old_change_end]
  292.  
  293. [change_begin]
  294. X3J13 voted in June 1988 (LAST-N)   to extend the last function to accept an
  295. optional second argument. The effect is to make last complementary in operation
  296. to butlast. The new description (with some additional examples) would be as
  297. follows.
  298.  
  299. [Function]
  300. last list &optional (n 1)
  301.  
  302. last returns the tail of the list consisting of the last n conses of list. The
  303. list may be a dotted list. It is an error if the list is circular.
  304.  
  305. The argument n must be a non-negative integer. If n is zero, then the atom that
  306. terminates the list is returned. If n is not less than the number of cons cells
  307. making up the list, then the list itself is returned.
  308.  
  309. For example:
  310.  
  311. (setq x '(a b c d))
  312. (last x) => (d)
  313. (rplacd (last x) '(e f))
  314. x => '(a b c d e f)
  315. (last x 3) => (d e f)
  316. (last '()) => ()
  317. (last '(a b c . d)) => (c . d)
  318. (last '(a b c . d) 0) => d
  319. (last '(a b c . d) 2) => (b c . d)
  320. (last '(a b c . d) 1729) => (a b c . d)
  321.  
  322. [change_end]
  323.  
  324. [Function]
  325. list &rest args
  326.  
  327. list constructs and returns a list of its arguments. For example:
  328.  
  329. (list 3 4 'a (car '(b . c)) (+ 6 -2)) => (3 4 a b 4)
  330.  
  331. (list) => ()
  332. (list (list 'a 'b) (list 'c 'd 'e)) => ((a b) (c d e))
  333.  
  334. [Function]
  335. list* arg &rest others
  336.  
  337. list* is like list except that the last cons of the constructed list is
  338. ``dotted.'' The last argument to list* is used as the cdr of the last cons
  339. constructed; this need not be an atom. If it is not an atom, then the effect is
  340. to add several new elements to the front of a list. For example:
  341.  
  342. (list* 'a 'b 'c 'd) => (a b c . d)
  343.  
  344. This is like
  345.  
  346. (cons 'a (cons 'b (cons 'c 'd)))
  347.  
  348. Also:
  349.  
  350. (list* 'a 'b 'c '(d e f)) => (a b c d e f)
  351. (list* x) == x
  352.  
  353. [Function]
  354. make-list size &key :initial-element
  355.  
  356. This creates and returns a list containing size elements, each of which is
  357. initialized to the :initial-element argument (which defaults to nil). size
  358. should be a non-negative integer. For example:
  359.  
  360. (make-list 5) => (nil nil nil nil nil)
  361. (make-list 3 :initial-element 'rah) => (rah rah rah)
  362.  
  363. [Function]
  364. append &rest lists
  365.  
  366. The arguments to append are lists. The result is a list that is the
  367. concatenation of the arguments. The arguments are not destroyed. For example:
  368.  
  369. (append '(a b c) '(d e f) '() '(g)) => (a b c d e f g)
  370.  
  371. Note that append copies the top-level list structure of each of its arguments
  372. except the last. The function concatenate can perform a similar operation, but
  373. always copies all its arguments. See also nconc, which is like append but
  374. destroys all arguments but the last.
  375.  
  376. The last argument actually need not be a list but may be any Lisp object, which
  377. becomes the tail end of the constructed list. For example, (append '(a b c) 'd)
  378. => (a b c . d).
  379.  
  380. (append x '()) is an idiom once frequently used to copy the list x, but the
  381. copy-list function is more appropriate to this task.
  382.  
  383. [Function]
  384. copy-list list
  385.  
  386. This returns a list that is equal to list, but not eq. Only the top level of
  387. list structure is copied; that is, copy-list copies in the cdr direction but
  388. not in the car direction. If the list is ``dotted,'' that is, (cdr (last list))
  389. is a non-nil atom, this will be true of the returned list also. See also
  390. copy-seq and copy-tree.
  391.  
  392. [Function]
  393. copy-alist list
  394.  
  395. copy-alist is for copying association lists. The top level of list structure of
  396. list is copied, just as for copy-list. In addition, each element of list that
  397. is a cons is replaced in the copy by a new cons with the same car and cdr.
  398.  
  399. [Function]
  400. copy-tree object
  401.  
  402. copy-tree is for copying trees of conses. The argument object may be any Lisp
  403. object. If it is not a cons, it is returned; otherwise the result is a new cons
  404. of the results of calling copy-tree on the car and cdr of the argument. In
  405. other words, all conses in the tree are copied recursively, stopping only when
  406. non-conses are encountered. Circularities and the sharing of substructure are
  407. not preserved.
  408.  
  409. -------------------------------------------------------------------------------
  410. Compatibility note: This function is called copy in Interlisp.
  411. -------------------------------------------------------------------------------
  412.  
  413. [Function]
  414. revappend x y
  415.  
  416. (revappend x y) is exactly the same as (append (reverse x) y) except that it is
  417. potentially more efficient. Both x and y should be lists. The argument x is
  418. copied, not destroyed. Compare this with nreconc, which destroys its first
  419. argument.
  420.  
  421. [Function]
  422. nconc &rest lists
  423.  
  424. nconc takes lists as arguments. It returns a list that is the arguments
  425. concatenated together. The arguments are changed rather than copied. (Compare
  426. this with append, which copies arguments rather than destroying them.) For
  427. example:
  428.  
  429. (setq x '(a b c))
  430. (setq y '(d e f))
  431. (nconc x y) => (a b c d e f)
  432. x => (a b c d e f)
  433.  
  434. Note, in the example, that the value of x is now different, since its last cons
  435. has been rplacd'd to the value of y. If one were then to evaluate (nconc x y)
  436. again, it would yield a piece of ``circular'' list structure, whose printed
  437. representation would be (a b c d e f d e f d e f ...), repeating forever; if
  438. the *print-circle* switch were non-nil, it would be printed as (a b c . #1=(d e
  439. f . #1#)).
  440.  
  441. [change_begin]
  442. X3J13 voted in March 1989 (REMF-DESTRUCTION-UNSPECIFIED)   to clarify the
  443. permissible side effects of certain operations. The side-effect behavior of
  444. nconc is specified by a recursive relationship outlined in the following table,
  445. in which a call to nconc matching the earliest possible pattern on the left is
  446. required to have side-effect behavior equivalent to the corresponding
  447. expression on the right.
  448.  
  449. (nconc)                 nil     ;No side effects
  450. (nconc nil . r)         (nconc . r)
  451. (nconc x)               x
  452. (nconc x y)             (let ((p x) (q y))
  453.                           (rplacd (last p) q)
  454.                           p)
  455. (nconc x y . r)         (nconc (nconc x y) . r)
  456.  
  457. [change_end]
  458.  
  459. [Function]
  460. nreconc x y
  461.  
  462. (nreconc x y) is exactly the same as (nconc (nreverse x) y) except that it is
  463. potentially more efficient. Both x and y should be lists. The argument x is
  464. destroyed. Compare this with revappend.
  465.  
  466. (setq planets '(jupiter mars earth venus mercury))
  467. (setq more-planets '(saturn uranus pluto neptune))
  468. (nreconc more-planets planets)
  469. => (neptune pluto uranus saturn jupiter mars earth venus mercury)
  470.   and now the value of more-planets is not well defined
  471.  
  472. [change_begin]
  473. X3J13 voted in March 1989 (REMF-DESTRUCTION-UNSPECIFIED)   to clarify the
  474. permissible side effects of certain operations; (nreconc x y) is permitted and
  475. required to have side-effect behavior equivalent to that of (nconc (nreverse x)
  476. y).
  477. [change_end]
  478.  
  479. [Macro]
  480. push item place
  481.  
  482. The form place should be the name of a generalized variable containing a list;
  483. item may refer to any Lisp object. The item is consed onto the front of the
  484. list, and the augmented list is stored back into place and returned. The form
  485. place may be any form acceptable as a generalized variable to setf. If the list
  486. held in place is viewed as a push-down stack, then push pushes an element onto
  487. the top of the stack. For example:
  488.  
  489. (setq x '(a (b c) d))
  490. (push 5 (cadr x)) => (5 b c)  and now x => (a (5 b c) d)
  491.  
  492. The effect of (push item place) is roughly equivalent to
  493.  
  494. (setf place (cons item place))
  495.  
  496. except that the latter would evaluate any subforms of place twice, while push
  497. takes care to evaluate them only once. Moreover, for certain place forms push
  498. may be significantly more efficient than the setf version.
  499.  
  500. [change_begin]
  501. X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER)   to clarify order of
  502. evaluation (see section 7.2). Note that item is fully evaluated before any part
  503. of place is evaluated.
  504. [change_end]
  505.  
  506. [Macro]
  507. pushnew item place &key :test :test-not :key
  508.  
  509. The form place should be the name of a generalized variable containing a list;
  510. item may refer to any Lisp object. If the item is not already a member of the
  511. list (as determined by comparisons using the :test predicate, which defaults to
  512. eql), then the item is consed onto the front of the list, and the augmented
  513. list is stored back into place and returned; otherwise the unaugmented list is
  514. returned. The form place may be any form acceptable as a generalized variable
  515. to setf. If the list held in place is viewed as a set, then pushnew adjoins an
  516. element to the set; see adjoin.
  517.  
  518. The keyword arguments to pushnew follow the conventions for the generic
  519. sequence functions. See chapter 14. In effect, these keywords are simply passed
  520. on to the adjoin function.
  521.  
  522. pushnew returns the new contents of the place. For example:
  523.  
  524. (setq x '(a (b c) d))
  525. (pushnew 5 (cadr x)) => (5 b c)   and now x => (a (5 b c) d)
  526. (pushnew 'b (cadr x)) => (5 b c)  and x is unchanged
  527.  
  528. The effect of
  529.  
  530. (pushnew item place :test p)
  531.  
  532. is roughly equivalent to
  533.  
  534. (setf place (adjoin item place :test p))
  535.  
  536. except that the latter would evaluate any subforms of place twice, while
  537. pushnew takes care to evaluate them only once. Moreover, for certain place
  538. forms pushnew may be significantly more efficient than the setf version.
  539.  
  540. [change_begin]
  541. X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER)   to clarify order of
  542. evaluation (see section 7.2). Note that item is fully evaluated before any part
  543. of place is evaluated.
  544. [change_end]
  545.  
  546. [Macro]
  547. pop place
  548.  
  549. The form place should be the name of a generalized variable containing a list.
  550. The result of pop is the car of the contents of place, and as a side effect the
  551. cdr of the contents is stored back into place. The form place may be any form
  552. acceptable as a generalized variable to setf. If the list held in place is
  553. viewed as a push-down stack, then pop pops an element from the top of the stack
  554. and returns it. For example:
  555.  
  556. (setq stack '(a b c))
  557. (pop stack) => a  and now stack => (b c)
  558.  
  559. The effect of (pop place) is roughly equivalent to
  560.  
  561. (prog1 (car place) (setf place (cdr place)))
  562.  
  563. except that the latter would evaluate any subforms of place three times, while
  564. pop takes care to evaluate them only once. Moreover, for certain place forms
  565. pop may be significantly more efficient than the setf version.
  566.  
  567. [change_begin]
  568. X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER)   to clarify order of
  569. evaluation (see section 7.2).
  570. [change_end]
  571.  
  572. [Function]
  573. butlast list &optional n
  574.  
  575. This creates and returns a list with the same elements as list, excepting the
  576. last n elements. n defaults to 1. The argument is not destroyed. If the list
  577. has fewer than n elements, then () is returned. For example:
  578.  
  579. (butlast '(a b c d)) => (a b c)
  580. (butlast '((a b) (c d))) => ((a b))
  581. (butlast '(a)) => ()
  582. (butlast nil) => ()
  583.  
  584. The name is from the phrase ``all elements but the last.''
  585.  
  586. [Function]
  587. nbutlast list &optional n
  588.  
  589. This is the destructive version of butlast; it changes the cdr of the cons n+1
  590. from the end of the list to nil. n defaults to 1. If the list has fewer than n
  591. elements, then nbutlast returns (), and the argument is not modified.
  592. (Therefore one normally writes (setq a (nbutlast a)) rather than simply
  593. (nbutlast a).) For example:
  594.  
  595. (setq foo '(a b c d))
  596. (nbutlast foo) => (a b c)
  597. foo => (a b c)
  598. (nbutlast '(a)) => ()
  599. (nbutlast 'nil) => ()
  600.  
  601. [Function]
  602. ldiff list sublist
  603.  
  604. list should be a list, and sublist should be a sublist of list, that is, one of
  605. the conses that make up list. ldiff (meaning ``list difference'') will return a
  606. new (freshly consed) list, whose elements are those elements of list that
  607. appear before sublist. If sublist is not a tail of list (and in particular if
  608. sublist is nil), then a copy of the entire list is returned. The argument list
  609. is not destroyed. For example:
  610.  
  611. (setq x '(a b c d e))
  612. (setq y (cdddr x)) => (d e)
  613. (ldiff x y) => (a b c)
  614.  
  615. but (ldiff '(a b c d) '(c d)) => (a b c d)
  616.  
  617. since the sublist was not eq to any part of the list.
  618.  
  619. -------------------------------------------------------------------------------
  620.  
  621. 15.3. Alteration of List Structure
  622.  
  623. The functions rplaca and rplacd may be used to make alterations in already
  624. existing list structure, that is, to change the car or cdr of an existing cons.
  625. One may also use setf in conjunction with car and cdr.
  626.  
  627. The structure is not copied but is destructively altered; hence caution should
  628. be exercised when using these functions, as strange side effects can occur if
  629. portions of list structure become shared. The nconc, nreverse, nreconc, and
  630. nbutlast functions, already described, have the same property, as do certain of
  631. the generic sequence functions such as delete. However, they are normally not
  632. used for this side effect; rather, the list-structure modification is purely
  633. for efficiency, and compatible non-modifying functions are provided.
  634.  
  635. [Function]
  636. rplaca x y
  637.  
  638. (rplaca x y) changes the car of x to y and returns (the modified) x. x must be
  639. a cons, but y may be any Lisp object. For example:
  640.  
  641. (setq g '(a b c))
  642. (rplaca (cdr g) 'd) => (d c)
  643. Now g => (a d c)
  644.  
  645. [Function]
  646. rplacd x y
  647.  
  648. (rplacd x y) changes the cdr of x to y and returns (the modified) x. x must be
  649. a cons, but y may be any Lisp object. For example:
  650.  
  651. (setq x '(a b c))
  652. (rplacd x 'd) => (a . d)
  653. Now x => (a . d)
  654.  
  655. [change_begin]
  656. The functions rplaca and rplacd go back to the earliest origins of Lisp, along
  657. with car, cdr, and cons. Nowadays, however, they seem to be falling by the
  658. wayside. More and more Common Lisp programmers use setf for nearly all
  659. structure modifications: (rplaca x y) is rendered as (setf (car x) y) or
  660. perhaps as (setf (first x) y). Even more likely is that a defstruct structure
  661. or a CLOS class is used in place of a list, if the data structure is at all
  662. complicated; in this case setf is used with a slot accessor.
  663. [change_end]
  664.  
  665. -------------------------------------------------------------------------------
  666.  
  667. 15.4. Substitution of Expressions
  668.  
  669.   A number of functions are provided for performing substitutions within a
  670. tree. All take a tree and a description of old subexpressions to be replaced by
  671. new ones. They come in non-destructive and destructive varieties and specify
  672. substitution either by two arguments or by an association list.
  673.  
  674. The naming conventions for these functions and for their keyword arguments
  675. generally follow the conventions for the generic sequence functions. See
  676. chapter 14.
  677.  
  678. [Function]
  679. subst new old tree &key :test :test-not :key
  680. subst-if new test tree &key :key
  681. subst-if-not new test tree &key :key
  682.  
  683. (subst new old tree) makes a copy of tree, substituting new for every subtree
  684. or leaf of tree (whether the subtree or leaf is a car or a cdr of its parent)
  685. such that old and the subtree or leaf satisfy the test. It returns the modified
  686. copy of tree. The original tree is unchanged, but the result tree may share
  687. with parts of the argument tree.
  688.  
  689. -------------------------------------------------------------------------------
  690. Compatibility note: In MacLisp, subst is guaranteed not to share with the tree
  691. argument, and the idiom (subst nil nil x) was used to copy a tree x. In Common
  692. Lisp, the function copy-tree should be used to copy a tree, as the subst idiom
  693. will not work.
  694. -------------------------------------------------------------------------------
  695.  
  696. For example:
  697.  
  698. (subst 'tempest 'hurricane
  699.        '(shakespeare wrote (the hurricane)))
  700.    => (shakespeare wrote (the tempest))
  701.  
  702. (subst 'foo 'nil '(shakespeare wrote (twelfth night)))
  703.    => (shakespeare wrote (twelfth night . foo) . foo)
  704.  
  705. (subst '(a . cons) '(old . pair)
  706.        '((old . spice) ((old . shoes) old . pair) (old . pair))
  707.        :test #'equal)
  708.    => ((old . spice) ((old . shoes) a . cons) (a . cons))
  709.  
  710. This function is not destructive; that is, it does not change the car or cdr of
  711. any already existing list structure. One possible definition of subst:
  712.  
  713. (defun subst (old new tree &rest x &key test test-not key)
  714.   (cond ((satisfies-the-test old tree :test test
  715.                              :test-not test-not :key key)
  716.          new)
  717.         ((atom tree) tree)
  718.         (t (let ((a (apply #'subst old new (car tree) x))
  719.                  (d (apply #'subst old new (cdr tree) x)))
  720.              (if (and (eql a (car tree))
  721.                       (eql d (cdr tree)))
  722.                  tree
  723.                  (cons a d))))))
  724.  
  725. See also substitute, which substitutes for top-level elements of a sequence.
  726.  
  727. [change_begin]
  728. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  729. user side effects; see section 7.9.
  730. [change_end]
  731.  
  732. [Function]
  733. nsubst new old tree &key :test :test-not :key
  734. nsubst-if new test tree &key :key
  735. nsubst-if-not new test tree &key :key
  736.  
  737. nsubst is a destructive version of subst. The list structure of tree is altered
  738. by destructively replacing with new each leaf or subtree of the tree such that
  739. old and the leaf or subtree satisfy the test.
  740.  
  741. [change_begin]
  742. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  743. user side effects; see section 7.9.
  744. [change_end]
  745.  
  746. [Function]
  747. sublis alist tree &key :test :test-not :key
  748.  
  749. sublis makes substitutions for objects in a tree (a structure of conses). The
  750. first argument to sublis is an association list. The second argument is the
  751. tree in which substitutions are to be made, as for subst. sublis looks at all
  752. subtrees and leaves of the tree; if a subtree or leaf appears as a key in the
  753. association list (that is, the key and the subtree or leaf satisfy the test),
  754. it is replaced by the object with which it is associated. This operation is
  755. non-destructive. In effect, sublis can perform several subst operations
  756. simultaneously. For example:
  757.  
  758. (sublis '((x . 100) (z . zprime))
  759.         '(plus x (minus g z x p) 4 . x))
  760.    => (plus 100 (minus g zprime 100 p) 4 . 100)
  761.  
  762. (sublis '(((+ x y) . (- x y)) ((- x y) . (+ x y)))
  763.         '(* (/ (+ x y) (+ x p)) (- x y))
  764.         :test #'equal)
  765.    => (* (/ (- x y) (+ x p)) (+ x y))
  766.  
  767. [change_begin]
  768. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  769. user side effects; see section 7.9.
  770. [change_end]
  771.  
  772. [Function]
  773. nsublis alist tree &key :test :test-not :key
  774.  
  775. nsublis is like sublis but destructively modifies the relevant parts of the
  776. tree.
  777.  
  778. [change_begin]
  779. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  780. user side effects; see section 7.9.
  781. [change_end]
  782.  
  783. -------------------------------------------------------------------------------
  784.  
  785. 15.5. Using Lists as Sets
  786.  
  787. Common Lisp includes functions that allow a list of items to be treated as a
  788. set. There are functions to add, remove, and search for items in a list, based
  789. on various criteria. There are also set union, intersection, and difference
  790. functions.
  791.  
  792. The naming conventions for these functions and for their keyword arguments
  793. generally follow the conventions that apply to the generic sequence functions.
  794. See chapter 14.
  795.  
  796. [Function]
  797. member item list &key :test :test-not :key
  798. member-if predicate list &key :key
  799. member-if-not predicate list &key :key
  800.  
  801. The list is searched for an element that satisfies the test. If none is found,
  802. nil is returned; otherwise, the tail of list beginning with the first element
  803. that satisfied the test is returned. The list is searched on the top level
  804. only. These functions are suitable for use as predicates.
  805.  
  806. For example:
  807.  
  808. (member 'snerd '(a b c d)) => nil
  809. (member-if #'numberp '(a #¥Space 5/3 foo)) => (5/3 foo)
  810. (member 'a '(g (a y) c a d e a f)) => (a d e a f)
  811.  
  812. Note, in the last example, that the value returned by member is eq to the
  813. portion of the list beginning with a. Thus rplaca on the result of member may
  814. be used to alter the found list element, if a check is first made that member
  815. did not return nil.
  816.  
  817. See also find and position.
  818.  
  819. [change_begin]
  820. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  821. user side effects; see section 7.9.
  822. [change_end]
  823.  
  824. -------------------------------------------------------------------------------
  825. Compatibility note: In MacLisp, the member function uses an equal comparison
  826. rather than eql, which is the default test for member in Common Lisp. Where in
  827. MacLisp one would write (member x y), in Common Lisp one must write (member x y
  828. :test #'equal) to get a completely identical effect. Similarly, one can get the
  829. precise effect, and no more, of the MacLisp (memq x y) by writing in Common
  830. Lisp (member x y :test #'eq).
  831. -------------------------------------------------------------------------------
  832.  
  833. [Function]
  834. tailp sublist list
  835.  
  836. This predicate is true if sublist is a sublist of list (that is, one of the
  837. conses that makes up list); otherwise it is false. Another way to look at this
  838. is that tailp is true if (nthcdr n list) is sublist, for some value of n. See
  839. ldiff.
  840.  
  841. [change_begin]
  842. X3J13 voted in January 1989 (TAILP-NIL)   to strike the parenthetical remark
  843. that suggests that the sublist must be a cons, to clarify that tailp is true if
  844. and only if there exists an integer n such that
  845.  
  846. (eql sublist (nthcdr n list))
  847.  
  848. and to specify that list may be a dotted list (implying that implementations
  849. must use atom and not endp to check for the end of the list).
  850. [change_end]
  851.  
  852. [Function]
  853. adjoin item list &key :test :test-not :key
  854.  
  855. adjoin is used to add an element to a set, provided that it is not already a
  856. member. The equality test defaults to eql.
  857.  
  858. (adjoin item list) == (if (member item list) list (cons item list))
  859.  
  860. In general, the test may be any predicate; the item is added to the list only
  861. if there is no element of the list that ``satisfies the test.''
  862.  
  863. adjoin deviates from the usual rules described in chapter 14 for the treatment
  864. of arguments named item and :key. If a :key function is specified, it is
  865. applied to item as well as to each element of the list. The rationale is that
  866. if the item is not yet in the list, it soon will be, and so the test is more
  867. properly viewed as being between two elements rather than between a separate
  868. item and an element.
  869.  
  870. (adjoin item list :key fn)
  871.   == (if (member (funcall fn item) list
  872.  
  873. See pushnew.
  874.  
  875. [change_begin]
  876. Notice of correction. In the first edition, the form (fn item) appeared in this
  877. example without the required funcall.
  878.  
  879. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  880. user side effects; see section 7.9.
  881. [change_end]
  882.  
  883. [Function]
  884. union list1 list2 &key :test :test-not :key
  885. nunion list1 list2 &key :test :test-not :key
  886.  
  887. union takes two lists and returns a new list containing everything that is an
  888. element of either of the lists. If there is a duplication between two lists,
  889. only one of the duplicate instances will be in the result. If either of the
  890. arguments has duplicate entries within it, the redundant entries may or may not
  891. appear in the result. For example:
  892.  
  893. (union '(a b c) '(f a d))
  894.    => (a b c f d) or (b c f a d) or (d f a b c) or ...
  895.  
  896. (union '((x 5) (y 6)) '((z 2) (x 4)) :key #'car)
  897.    => ((x 5) (y 6) (z 2)) or ((x 4) (y 6) (z 2)) or ...
  898.  
  899. There is no guarantee that the order of elements in the result will reflect the
  900. ordering of the arguments in any particular way. The implementation is
  901. therefore free to use any of a variety of strategies. The result list may share
  902. cells with, or be eq to, either of the arguments if appropriate.
  903.  
  904. In general, the test may be any predicate, and the union operation may be
  905. described as follows. For all possible ordered pairs consisting of one element
  906. from list1 and one element from list2, the test is used to determine whether
  907. they ``match.'' For every matching pair, at least one of the two elements of
  908. the pair will be in the result. Moreover, any element from either list that
  909. matches no element of the other will appear in the result. All this is very
  910. general, but probably not particularly useful unless the test is an equivalence
  911. relation.
  912.  
  913. The :test-not argument can be useful when the test function is the logical
  914. negation of an equivalence test. A good example of this is the function
  915. mismatch, which is logically inverted so that possibly useful information can
  916. be returned if the arguments do not match. This additional ``useful
  917. information'' is discarded in the following example; mismatch is used purely as
  918. a predicate.
  919.  
  920. (union '(#(a b) #(5 0 6) #(f 3))
  921.        '(#(5 0 6) (a b) #(g h))
  922.        :test-not
  923.        #'mismatch)
  924.    => (#(a b) #(5 0 6) #(f 3) #(g h))     ;One possible result
  925.    => ((a b) #(f 3) #(5 0 6) #(g h))      ;Another possible result
  926.  
  927. Using :test-not #'mismatch differs from using :test #'equalp, for example,
  928. because mismatch will determine that #(a b) and (a b) are the same, while
  929. equalp would regard them as not the same.
  930.  
  931. nunion is the destructive version of union. It performs the same operation but
  932. may destroy the argument lists, perhaps in order to use their cells to
  933. construct the result.
  934.  
  935. [change_begin]
  936. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  937. user side effects; see section 7.9.
  938.  
  939. X3J13 voted in March 1989 (REMF-DESTRUCTION-UNSPECIFIED)   to clarify the
  940. permissible side effects of certain operations; nunion is permitted to perform
  941. a setf on any part, car or cdr, of the top-level list structure of any of the
  942. argument lists.
  943. [change_end]
  944.  
  945. [Function]
  946. intersection list1 list2 &key :test :test-not :key
  947. nintersection list1 list2 &key :test :test-not :key
  948.  
  949. intersection takes two lists and returns a new list containing everything that
  950. is an element of both argument lists. If either list has duplicate entries, the
  951. redundant entries may or may not appear in the result. For example:
  952.  
  953. (intersection '(a b c) '(f a d)) => (a)
  954.  
  955. There is no guarantee that the order of elements in the result will reflect the
  956. ordering of the arguments in any particular way. The implementation is
  957. therefore free to use any of a variety of strategies. The result list may share
  958. cells with, or be eq to, either of the arguments if appropriate.
  959.  
  960. In general, the test may be any predicate, and the intersection operation may
  961. be described as follows. For all possible ordered pairs consisting of one
  962. element from list1 and one element from list2, the test is used to determine
  963. whether they ``match.'' For every matching pair, exactly one of the two
  964. elements of the pair will be put in the result. No element from either list
  965. appears in the result that does not match an element from the other list. All
  966. this is very general, but probably not particularly useful unless the test is
  967. an equivalence relation.
  968.  
  969. nintersection is the destructive version of intersection. It performs the same
  970. operation, but may destroy list1, perhaps in order to use its cells to
  971. construct the result. (The argument list2 is not destroyed.)
  972.  
  973. [change_begin]
  974. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  975. user side effects; see section 7.9.
  976.  
  977. X3J13 voted in March 1989 (REMF-DESTRUCTION-UNSPECIFIED)   to clarify the
  978. permissible side effects of certain operations; nintersection is permitted to
  979. perform a setf on any part, car or cdr, of the top-level list structure of any
  980. of the argument lists.
  981. [change_end]
  982.  
  983. [Function]
  984. set-difference list1 list2 &key :test :test-not :key
  985. nset-difference list1 list2 &key :test :test-not :key
  986.  
  987. set-difference returns a list of elements of list1 that do not appear in list2.
  988. This operation is not destructive.
  989.  
  990. There is no guarantee that the order of elements in the result will reflect the
  991. ordering of the arguments in any particular way. The implementation is
  992. therefore free to use any of a variety of strategies. The result list may share
  993. cells with, or be eq to, either of the arguments if appropriate.
  994.  
  995. In general, the test may be any predicate, and the set difference operation may
  996. be described as follows. For all possible ordered pairs consisting of one
  997. element from list1 and one element from list2, the test is used to determine
  998. whether they ``match.'' An element of list1 appears in the result if and only
  999. if it does not match any element of list2. This is very general and permits
  1000. interesting applications. For example, one can remove from a list of strings
  1001. all those strings containing one of a given list of characters:
  1002.  
  1003. ;; Remove all flavor names that contain "c" or "w".
  1004. (set-difference '("strawberry" "chocolate" "banana"
  1005.                   "lemon" "pistachio" "rhubarb")
  1006.                 '(#¥c #¥w)
  1007.                 :test
  1008.                 #'(lambda (s c) (find c s)))
  1009.    => ("banana" "rhubarb" "lemon")     ;One possible ordering
  1010.  
  1011. nset-difference is the destructive version of set-difference. This operation
  1012. may destroy list1.
  1013.  
  1014. [change_begin]
  1015. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1016. user side effects; see section 7.9.
  1017. [change_end]
  1018.  
  1019. -------------------------------------------------------------------------------
  1020. Compatibility note: An approximately equivalent Interlisp function is
  1021. ldifference.
  1022. -------------------------------------------------------------------------------
  1023.  
  1024. [Function]
  1025.  
  1026. set-exclusive-or list1 list2 &key :test :test-not :key
  1027. nset-exclusive-or list1 list2 &key :test :test-not :key
  1028.  
  1029. set-exclusive-or returns a list of elements that appear in exactly one of list1
  1030. and list2. This operation is not destructive.
  1031.  
  1032. There is no guarantee that the order of elements in the result will reflect the
  1033. ordering of the arguments in any particular way. The implementation is
  1034. therefore free to use any of a variety of strategies. The result list may share
  1035. cells with, or be eq to, either of the arguments if appropriate.
  1036.  
  1037. In general, the test may be any predicate, and the set-exclusive-or operation
  1038. may be described as follows. For all possible ordered pairs consisting of one
  1039. element from list1 and one element from list2, the test is used to determine
  1040. whether they ``match.'' The result contains precisely those elements of list1
  1041. and list2 that appear in no matching pair.
  1042.  
  1043. nset-exclusive-or is the destructive version of set-exclusive-or. Both lists
  1044. may be destroyed in producing the result.
  1045.  
  1046. [change_begin]
  1047. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1048. user side effects; see section 7.9.
  1049.  
  1050. X3J13 voted in March 1989 (REMF-DESTRUCTION-UNSPECIFIED)   to clarify the
  1051. permissible side effects of certain operations; nset-exclusive-or is permitted
  1052. to perform a setf on any part, car or cdr, of the top-level list structure of
  1053. any of the argument lists.
  1054. [change_end]
  1055.  
  1056. [Function]
  1057. subsetp list1 list2 &key :test :test-not :key
  1058.  
  1059. subsetp is a predicate that is true if every element of list1 appears in
  1060. (``matches'' some element of) list2, and false otherwise.
  1061.  
  1062. [change_begin]
  1063. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1064. user side effects; see section 7.9.
  1065. [change_end]
  1066.  
  1067. -------------------------------------------------------------------------------
  1068.  
  1069. 15.6. Association Lists
  1070.  
  1071. An association list, or a-list, is a data structure used very frequently in
  1072. Lisp. An a-list is a list of pairs (conses); each pair is an association. The
  1073. car of a pair is called the key, and the cdr is called the datum.
  1074.  
  1075. An advantage of the a-list representation is that an a-list can be
  1076. incrementally augmented simply by adding new entries to the front. Moreover,
  1077. because the searching function assoc searches the a-list in order, new entries
  1078. can ``shadow'' old entries. If an a-list is viewed as a mapping from keys to
  1079. data, then the mapping can be not only augmented but also altered in a
  1080. non-destructive manner by adding new entries to the front of the a-list.
  1081.  
  1082. Sometimes an a-list represents a bijective mapping, and it is desirable to
  1083. retrieve a key given a datum. For this purpose, the ``reverse'' searching
  1084. function rassoc is provided. Other variants of a-list searches can be
  1085. constructed using the function find or member.
  1086.  
  1087. It is permissible to let nil be an element of an a-list in place of a pair.
  1088. Such an element is not considered to be a pair but is simply passed over when
  1089. the a-list is searched by assoc.
  1090.  
  1091. [Function]
  1092. acons key datum a-list
  1093.  
  1094. acons constructs a new association list by adding the pair (key . datum) to the
  1095. old a-list.
  1096.  
  1097. (acons x y a) == (cons (cons x y) a)
  1098.  
  1099. [change_begin]
  1100. This is a trivial convenience function, but I find I use it a lot.
  1101. [change_end]
  1102.  
  1103. [Function]
  1104. pairlis keys data &optional a-list
  1105.  
  1106. pairlis takes two lists and makes an association list that associates elements
  1107. of the first list to corresponding elements of the second list. It is an error
  1108. if the two lists keys and data are not of the same length. If the optional
  1109. argument a-list is provided, then the new pairs are added to the front of it.
  1110.  
  1111. The new pairs may appear in the resulting a-list in any order; in particular,
  1112. either forward or backward order is permitted. Therefore the result of the call
  1113.  
  1114. (pairlis '(one two) '(1 2) '((three . 3) (four . 19)))
  1115.  
  1116. might be
  1117.  
  1118. ((one . 1) (two . 2) (three . 3) (four . 19))
  1119.  
  1120. but could equally well be
  1121.  
  1122. ((two . 2) (one . 1) (three . 3) (four . 19))
  1123.  
  1124. [Function]
  1125. assoc item a-list &key :test :test-not :key
  1126. assoc-if predicate a-list
  1127. assoc-if-not predicate a-list
  1128.  
  1129. [change_begin]
  1130. X3J13 voted in March 1988 (ASSOC-RASSOC-IF-KEY)   to allow assoc-if and
  1131. assoc-if-not also to take a keyword argument named :key, to be used to
  1132. determine whether a pair ``satisfies the test'' in the same manner as for
  1133. sequence functions. The new function descriptions are therefore as follows:
  1134.  
  1135. [Function]
  1136. assoc-if predicate a-list &key :key
  1137. assoc-if-not predicate a-list &key :key
  1138.  
  1139. The omission of :key arguments for these functions in the first edition was
  1140. probably an oversight.
  1141. [change_end]
  1142.  
  1143. Each of these searches the association list a-list. The value is the first pair
  1144. in the a-list such that the car of the pair satisfies the test, or nil if there
  1145. is no such pair in the a-list. For example:
  1146.  
  1147. (assoc 'r '((a . b) (c . d) (r . x) (s . y) (r . z)))
  1148.         =>  (r . x)
  1149. (assoc 'goo '((foo . bar) (zoo . goo))) => nil
  1150. (assoc '2 '((1 a b c) (2 b c d) (-7 x y z))) => (2 b c d)
  1151.  
  1152. It is possible to rplacd the result of assoc provided that it is not nil, in
  1153. order to ``update'' the ``table'' that was assoc's second argument. (However,
  1154. it is often better to update an a-list by adding new pairs to the front, rather
  1155. than altering old pairs.) For example:
  1156.  
  1157. (setq values '((x . 100) (y . 200) (z . 50)))
  1158. (assoc 'y values) => (y . 200)
  1159. (rplacd (assoc 'y values) 201)
  1160. (assoc 'y values) => (y . 201) now
  1161.  
  1162. A typical trick is to say (cdr (assoc x y)). Because the cdr of nil is
  1163. guaranteed to be nil, this yields nil if no pair is found or if a pair is found
  1164. whose cdr is nil. This is useful if nil serves its usual role as a ``default
  1165. value.''
  1166.  
  1167. The two expressions
  1168.  
  1169. (assoc item list :test fn)
  1170.  
  1171. and
  1172.  
  1173. (find item list :test fn :key #'car)
  1174.  
  1175. are equivalent in meaning with one important exception: if nil appears in the
  1176. a-list in place of a pair, and the item being searched for is nil, find will
  1177. blithely compute the car of the nil in the a-list, find that it is equal to the
  1178. item, and return nil, whereas assoc will ignore the nil in the a-list and
  1179. continue to search for an actual pair (cons) whose car is nil. See find and
  1180. position.
  1181.  
  1182. [change_begin]
  1183. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1184. user side effects; see section 7.9.
  1185. [change_end]
  1186.  
  1187. -------------------------------------------------------------------------------
  1188. Compatibility note: In MacLisp, the assoc function uses an equal comparison
  1189. rather than eql, which is the default test for assoc in Common Lisp. Where in
  1190. MacLisp one would write (assoc x y), in Common Lisp one must write (assoc x y
  1191. :test #'equal) to get the completely identical effect. Similarly, one can get
  1192. the precise effect, and no more, of the MacLisp (assq x y) by writing in Common
  1193. Lisp (assoc x y :test #'eq).
  1194. -------------------------------------------------------------------------------
  1195.  
  1196. In Interlisp, assoc uses an eq test, and sassoc uses an Interlisp equal test.
  1197.  
  1198. [Function]
  1199. rassoc item a-list &key :test :test-not :key
  1200. rassoc-if predicate a-list
  1201. rassoc-if-not predicate a-list
  1202.  
  1203. [change_begin]
  1204. X3J13 voted in March 1988 (ASSOC-RASSOC-IF-KEY)   to allow rassoc-if and
  1205. rassoc-if-not also to take a keyword argument named :key, to be used to
  1206. determine whether a pair ``satisfies the test'' in the same manner as for
  1207. sequence functions. The new function descriptions are therefore as follows:
  1208.  
  1209. [Function]
  1210. rassoc-if predicate a-list &key :key
  1211. rassoc-if-not predicate a-list &key :key
  1212.  
  1213. The omission of :key arguments for these functions in the first edition was
  1214. probably an oversight.
  1215. [change_end]
  1216.  
  1217. rassoc is the reverse form of assoc; it searches for a pair whose cdr satisfies
  1218. the test, rather than the car. If the a-list is considered to be a mapping,
  1219. then rassoc treats the a-list as representing the inverse mapping. For example:
  1220.  
  1221. (rassoc 'a '((a . b) (b . c) (c . a) (z . a))) => (c . a)
  1222.  
  1223. The expressions
  1224.  
  1225. (rassoc item list :test fn)
  1226.  
  1227. and
  1228.  
  1229. (find item list :test fn :key #'cdr)
  1230.  
  1231. are equivalent in meaning, except when the item is nil and nil appears in place
  1232. of a pair in the a-list. See the discussion of the function assoc.
  1233.  
  1234. [change_begin]
  1235. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1236. user side effects; see section 7.9.
  1237. [change_end]
  1238.  
  1239. -------------------------------------------------------------------------------
  1240.  
  1241.  
  1242.  
  1243.  
  1244.